package com.ct.rabbitmqspringboot.controller;

import com.ct.rabbitmqspringboot.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/producer")
@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 工作模式
    @RequestMapping("/work")
    public String work() {

        // 使用内置交换机, RoutingKey 和队列名称一致
        rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, "hello spring amqp : work...");
        return "发送成功";

    }

    // 发布订阅模式
    @RequestMapping("/fanout")
    public String fanout(){
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"", "hello spring amqp:fanout...");
        return "发送成功";
    }

    // 路由模式
    // @PathVariable : 取路径中的变量
    @RequestMapping("/direct/{routingKey}")
    public String direct(@PathVariable("routingKey") String routingKey){
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, routingKey, "hello spring amqp:direct, my routing key is " + routingKey);
        return "发送成功";
    }

    // 通配符模式
    @RequestMapping("/topic/{routingKey}")
    public String topic(@PathVariable("routingKey") String routingKey){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "hello spring amqp:topic, my routing key is " + routingKey);
        return "发送成功";
    }

    // 推模式
    @RequestMapping("/push")
    public void push() {
        this.rabbitTemplate.convertAndSend(Constants.PUSH_EXCHANGE, "push", "hello push...");
        System.out.println("推模式消息发送成功!");
    }

    // 拉模式
    @RequestMapping("/pull")
    public void pull() {
        this.rabbitTemplate.convertAndSend(Constants.PULL_EXCHANGE, "pull", "hello pull...");
        System.out.println("拉模式消息发送成功!");
    }
}
